(function() {
    base2Cells.util.namespace("base2Cells.ytPlaylist");

    // ---- Imports ----
    var Video;
    var Global;

    try {
        Video = base2Cells.ytPlaylist.Video;
        Global = base2Cells.ytPlaylist.Global;
    } catch(e) {
        alert("Import Error: Could not find necessary libraries within Item");
    }

    /**
     * @param cfg The configuration file, containing the following attributes.
     * <pre>
     *  {
     *      artist: The artist's name.
     *      title: The tile of the video.
     *      ytUrl: The youtube url.
     *      ownerCt: The owner container.
     *      id: The id number for this item.
     *  }
     * </pre>
     */
    base2Cells.ytPlaylist.Item = function(cfg) {
        /** The artist name. */
        this.artist = "";
        /** The title of the video. */
        this.title = "";
        /** The url of the youtube video. */
        this.ytUrl = "";
        /** The id of the youtube video. */
        this.ytId = "";
        /** The generated element. */
        this.el = null;
        /** The video object. */
        this.video = null;
        /** The owner container. */
        this.ownerCt = null;

        // Constructor
        (function(cfg) {
            // This objects variables.
            this.artist = cfg.artist;
            this.title = cfg.title;
            this.ytUrl = cfg.ytUrl;
            this.ownerCt = cfg.ownerCt;
            this.id = cfg.id;

            // Create the item dom element.
            this.el = document.createElement('div');
            this.el.setAttribute("class", "item");
            this.el.setAttribute("id", "item-" + this.id);
            this.el.setAttribute("idNum", this.id);

            var spacerEl = document.createElement('span');
            spacerEl.setAttribute('class', 'itemSpacer');
            spacerEl.innerHTML = "";

            var artistEl = document.createElement('span');
            artistEl.setAttribute('class', 'itemArtist');
            artistEl.innerHTML = this.artist;

            var hyphenEl = document.createElement('span');

            hyphenEl.innerHTML = " - ";

            var titleEl = document.createElement('span');
            titleEl.setAttribute('class', 'itemTitle');
            titleEl.innerHTML = this.title;

            var itemDelete = document.createElement('span');
            itemDelete.setAttribute('class', 'itemDelete');

            var scope = this;

            itemDelete.onclick = function(cfg) {
                Global.iAddedSong = true;
                scope.removeSelf();
            };

            itemDelete.innerHTML = "X";

            this.el.appendChild(spacerEl);
            this.el.appendChild(artistEl);
            this.el.appendChild(hyphenEl);
            this.el.appendChild(titleEl);
            this.el.appendChild(itemDelete);

            // Create an onclick function that will show the video.
            var scope = this;
            function clickHandler() {
                scope.showVideo();
            }

            spacerEl.onclick = clickHandler;
            artistEl.onclick = clickHandler;
            hyphenEl.onclick = clickHandler;
            titleEl.onclick = clickHandler;

            // Parse out the id of the url.
            if (this.ytUrl.match(/http:\/\/www.youtube.com.*/)) {
                this.ytId = this.ytUrl.replace(/.*v=([^&]*).*/g , "$1");
            } else {
                this.ytId = this.ytUrl;
            }

            // Create the video element associated with this item.
            this.video = new Video({
                id: this.ytId,
                idNum: this.id,
                ownerCt: this.el
            });
        }).apply(this, [cfg]);

        /** @return The element for this item. */
        this.getEl = function() {
            return this.el;
        }

        /**
         * Show the video of this item.
         */
        this.showVideo = function() {
            if (Global.currentShowingItem == this) {
                this.hideVideo();
                Global.currentShowingItem = null;
            } else {
                Global.currentShowingItem && Global.currentShowingItem.hideVideo();
                this.video.apply();
                Global.currentShowingItem = this;
            }
        }

        /**
         * Remove this video.
         */
        this.hideVideo = function() {
            this.video.remove();
        }

        /** @return This id number. */
        this.getId = function() {
            return this.id;
        }

        this.removeSelf = function() {
            this.ownerCt.removeChild(this.el);
            base2Cells.ytPlaylist.removeItem(this);
            var scope = this;
            if (wave.isInWaveContainer()) {
                var state = wave.getState();
                state.submitValue('listChange', wave.util.printJson({
                    artist: scope.artist,
                    title: scope.title,
                    ytUrl: scope.ytUrl,
                    itemId: scope.id,
                    add: false
                }));
            }
        }
    }
})();
